package com.feng.sharedingjdbc.web;

import com.alibaba.nacos.api.model.v2.Result;
import com.feng.sharedingjdbc.model.OrderEntity;
import com.feng.sharedingjdbc.rpc.UserService;
import com.feng.sharedingjdbc.rpc.dto.UserDto;
import com.feng.sharedingjdbc.service.OrderService;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author yinglong.zhang
 * @description
 * @date 2024-1-29 13:56
 */
@Slf4j
@RestController
@RequestMapping("/orders")
public class OrderController {


    private final OrderService orderService;
    private final UserService userService;

    public OrderController(OrderService orderService, UserService userService) {
        this.orderService = orderService;
        this.userService = userService;
    }

    @PostMapping("/add")
    @GlobalTransactional(rollbackFor = Exception.class)
    public Result<OrderEntity> create(@RequestBody OrderEntity record) {
        OrderEntity orderEntity = null;
        try {
            log.info("分布式事务id:{}", RootContext.getXID());
            orderEntity = orderService.insert(record);

            UserDto userDto = new UserDto();
            userDto.setId(orderEntity.getUserId());
            userDto.setScore(orderEntity.getAmount().multiply(new BigDecimal(1000)).longValue());
//            Result<Void> voidResult = userService.addScore(RootContext.getXID(), userDto);
            Result<Void> voidResult = userService.addScore(userDto);
            if(voidResult.getCode() != 0) {
                throw new RuntimeException("新增订单失败");
            }
            if (orderEntity.getUserId() == 2) {
                throw new RuntimeException("测试分布式事务");
            }
        } catch (RuntimeException e) {
            try {
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            } catch (TransactionException ex) {
                ex.printStackTrace();
            }
            throw new RuntimeException(e);
        }
        return Result.success(orderEntity);
    }

    @PostMapping("/add/notDistribute")
    public Result<OrderEntity> createWithoutTransaction(@RequestBody OrderEntity record) {
        OrderEntity orderEntity = orderService.insert(record);

        UserDto userDto = new UserDto();
        userDto.setId(orderEntity.getUserId());
        userDto.setScore(orderEntity.getAmount().multiply(new BigDecimal(1000)).longValue());
        Result<Void> voidResult = userService.addScore(userDto);
        if(voidResult.getCode() != 0) {
            throw new RuntimeException("新增订单失败");
        }
        if (orderEntity.getUserId() == 2) {
            throw new RuntimeException("测试分布式事务");
        }
        return Result.success(orderEntity);
    }


    @PostMapping("/update")
    public Result<OrderEntity> update(OrderEntity record) {
        OrderEntity orderEntity = orderService.update(record);
        return Result.success(orderEntity);
    }


}